package com.game.server.message.proto.execute;

import com.comm.ISession;

import com.game.majiang.config.DataManager;
import com.game.majiang.logic.LogicClient;
import com.game.majiang.logic.LogicServer;
import com.game.server.db.dao.XyNotificationLogDao;
import com.game.server.db.entity.XyClubInfo;
import com.game.server.db.entity.XyClubMemberInfo;
import com.game.server.db.entity.XyNotificationLog;
import com.game.server.message.proto.in.AbstractXYclubjoinReq;
import com.game.server.message.proto.out.XYInformDataAlterRsp;
import com.game.server.message.proto.out.XYclubinfoStruct;
import com.game.server.message.proto.out.XYclubjoinRsp;
import com.game.server.message.proto.out.XYclubserchRsp;
import com.game.server.util.NoticeUtil;
import net.sf.json.JSONObject;

public class XYclubjoinReq extends AbstractXYclubjoinReq {

    @Override
    public void Execute(ISession session) {
        LogicClient client = (LogicClient) LogicServer.getInstance().getClient(session.getId());
        if (client != null&&client.checkClick(msgcode)) {
            XYclubjoinRsp rsp = new XYclubjoinRsp();
            rsp.setIsok(3);
            XyClubInfo clubInfo = DataManager.getInstance().getMapClub().get(this.id);
            if (clubInfo!=null){
                rsp.setIsok(2);
                XyClubMemberInfo player = clubInfo.getPlayer().get(client.getUserId());
                if (player==null){
                    rsp.setIsok(1);
                    int applyCount = XyNotificationLogDao.findByUidApplyCount(client.getUserId(), this.id,1,1);

                    if (applyCount==0){
                        rsp.setIsok(0);
                        XyNotificationLog entity = new XyNotificationLog();
                        entity.setCid(clubInfo.getId());
                        entity.setUid(client.getUserId());
                        entity.setNotification_type(1);
                        entity.setMsg_type(1);
                        entity.setState(0);
                        JSONObject additionalData = new JSONObject();
                        additionalData.put("searchType",this.type);
                        additionalData.put("searchid",this.searchid);
                        entity.setAdditional_data(additionalData.toString());
                        XyNotificationLogDao.insertInto(entity);

                        NoticeUtil.ClubNoticeNotice(client, clubInfo);
                    }
                }else{
                    if (player.getDel_type()==1){
                        rsp.setIsok(1);
                        int applyCount = XyNotificationLogDao.findByUidApplyCount(client.getUserId(), this.id,1,1);
                        if (applyCount==0){
                            rsp.setIsok(0);
                            XyNotificationLog entity = new XyNotificationLog();
                            entity.setCid(clubInfo.getId());
                            entity.setUid(client.getUserId());
                            entity.setNotification_type(1);
                            entity.setMsg_type(1);
                            entity.setState(0);
                            JSONObject additionalData = new JSONObject();
                            additionalData.put("searchType",this.type);
                            additionalData.put("searchid",this.searchid);
                            entity.setAdditional_data(additionalData.toString());
                            XyNotificationLogDao.insertInto(entity);

                            NoticeUtil.ClubNoticeNotice(client, clubInfo);
                        }
                    }
                }
            }
            client.Send(rsp);
        }
    }
}
